package com.reportingapplication.student;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.reportingapplication.student.R;
import com.reportingapplication.student.tools.*;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LocationSelectionActivity extends Activity {

	/** Shared Preferences file**/
	private SharedPreferences  settings;
	private EditText window;
	private String location;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_selection);
		window = (EditText) findViewById(R.id.editText1);
		window.setText("Kies een lokaal in de buurt:");
		settings = getSharedPreferences(ID.PREFS_NAME, 0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.location_selection, menu);
		return true;
	}
	
	public void handleButtonPress(View view){
		Button button = (Button) findViewById(view.getId());
		String windowText = window.getText().toString();
		String inputText = button.getText().toString();
		if(windowText.equals("Kies een lokaal in de buurt:")){
			window.setText(inputText);
		} else if(windowText.length() < 3 && inputText.length() == 1){
			window.append(inputText);
		}
	}
	
	public void handleRestartPress(View view){
		window.setText("Kies een lokaal in de buurt:");
	}
	
	public void handleOKPress(View view){
		//Send out help message
		if(correctTiming()){
    		// Button was pressed during school time, so send a request to the teachers.
			// Get the location
			location = window.getText().toString();
			// Check if anything was filled in
			if(location.equals("Kies een lokaal in de buurt:")){
				Toast.makeText(this, "Vergeet niet een locatie in te vullen.", Toast.LENGTH_LONG).show();
			}else{
				// Start an event
				(new StartEventTask()).execute();
			}
			Toast.makeText(this, "Er is een melding gestuurd, er komt zo een docent aan!", Toast.LENGTH_LONG).show();
    	}else{
			// toast a message that button can only be pressed during school operating hours
			Toast.makeText(this, "Button can only be used during school operating hours", Toast.LENGTH_LONG).show();
		}
		//Show message and then finish task
		finish();
	}
	
	public void handleBackPress(View view){
		String windowText = window.getText().toString();
		if(windowText.equals("Kies een lokaal in de buurt:")){
			this.finish();
		}else{
			String newText;
			if(windowText.length() == 1 || windowText.length() >= 4){
				newText = "Kies een lokaal in de buurt:";
			}else{
				newText = windowText.substring(0, windowText.length()-1);
			}
			window.setText(newText);
		}
	}
	
	/**
     * Check whether the button is pressed during school operating hours
     * @return whether the time is within school operating hours.
     */
    private boolean correctTiming(){
    	boolean result = false;
    	Calendar calendar = Calendar.getInstance();
    	// Get the current date and time
    	int weekday = calendar.get(Calendar.DAY_OF_WEEK);
    	int hour = calendar.get(Calendar.HOUR_OF_DAY);
    	int minute = calendar.get(Calendar.MINUTE);
    	// Perform the checks
    	if(weekday >= Calendar.MONDAY && weekday <= Calendar.FRIDAY ){
    		if(hour >= 8 && hour <= 16){
    			if((hour == 8 && minute < 10) || (hour == 16 && minute >= 30)){
    				
    			}else{
    				result = true;
    			}
    			
    		}
    	}
    	return result;
    }
    
    /**
     * Called when a teacher needs to be notified of the button press
     */
	private class StartEventTask extends AsyncTask<String, String, String>{
		@Override
		protected String doInBackground(String... args) {	 
	    	// Create a list with the values to be sent to the database
	    	List<NameValuePair> params = new ArrayList<NameValuePair>();
	    	// Send the event_id and the starting time of the notification
	    	params.add(new BasicNameValuePair(ID.STUDENT_NAME, settings.getString(ID.STUDENT_NAME, "")));
	    	params.add(new BasicNameValuePair(ID.LOCATION, location));
	    	JSONParser jparser = new JSONParser();
	    	jparser.makeHttpRequest(ID.URL_NOTIFY, "GET", params);	
	    	location = "";
	    	return "Succes!";
		}
	}
}
